import router, { resetRouter } from '@/router'
import store from './store'
import Cookies from 'js-cookie'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import companycertification from './directive/company-certification' // 资质认证弹窗全局组件
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration
const WhiteList = ['/login', '/yhtMember', 'teacher'] // 白名单路由
router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()
  const query = Object.assign({}, to.query)
  // console.log(query, 'query');
  query.redirect = from.path

  // set page title
  //   document.title = getPageTitle(to.meta.title)
  // 从cookie中获取厂商信息
  const passwordValid = Cookies.get('passwordValid')
  const username = Cookies.get('username')
  const mobile = Cookies.get('mobile')
  const CheckValid = Cookies.get('CheckValid')
  // console.log(to.path, 'to.path');
  if (WhiteList.indexOf(to.path) > -1) {
    // console.log('进入beforeeach2')
    if (mobile && passwordValid && username && CheckValid) {
      // console.log('进入beforeeach1')
      // -1：已退回  0：待审核 1：直接进入 2：未上传资质 3：弹窗选择一个账号进入  4：新用户弹注册表单
      const ficationData = await store.dispatch('user/getFicationStatus')
      // ficationData.authorityStatus = 1
      if (ficationData.authorityStatus != 1) {
        if (ficationData.authorityStatus == 4) {

          if (to.path == '/login') {
            // console.log('进入beforeeach3')
            next()
            NProgress.done()
          } else {
            // console.log('进入beforeeach4')
            next({ path: '/login?type=information', query: query })
            NProgress.done()
          }
        } else {
          //console.log('进入beforeeach401')
          companycertification.install({ open: true, ficationData: ficationData, router: router, store: store })
          NProgress.done()
        }
      } else {
        // console.log('进入beforeeach666')
        if (query.type == 'teacher') {
          next()
          NProgress.done()
        } else {
          // 有账号之后禁止进入白名单路由
          next('/surey')
          NProgress.done()
        }

      }
    } else {
      next()
      NProgress.done()
    }
  } else {
    if (mobile && passwordValid && username && CheckValid) {
      //console.log('进入beforeeach1')
      // -1：已退回  0：待审核 1：直接进入 2：未上传资质 3：弹窗选择一个账号进入  4：新用户弹注册表单
      const ficationData = await store.dispatch('user/getFicationStatus')
      // ficationData.authorityStatus = 1
      if (ficationData.authorityStatus != 1) {
        if (ficationData.authorityStatus == 4) {
          if (to.path == '/login') {
            next()
            NProgress.done()
          } else {
            next({ path: '/login?type=information', query: query })
            NProgress.done()
          }
        } else {
          //console.log('进入beforeeach402')
          companycertification.install({ open: true, ficationData: ficationData, router: router, store: store })
          NProgress.done()
        }
      } else {
        await store.dispatch('root/setCompanyByUserInfo')
        next()
        NProgress.done()
      }
    } else {
      //console.log('进入beforeeach5')
      next({ path: '/login?type=sign', query: query })
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
router.onError((error) => {
  const pattern = 'Loading chunk'
  const pattern2 = 'Loading CSS chunk'
  const isChunkLoadFailed = error.message.indexOf(pattern) >= 0 || error.message.indexOf(pattern2) >= 0
  const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    router.replace(targetPath)
  }
})
